package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.MallBoomService;
import cc.linker.steplink.crm.merchanth5.service.OverViewInfoService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/h5/overview")
public class OverViewInfoController {

    /**
     * 访客
     */
    public static final String GUEST = "OVERVIEW:GUEST";
    /**
     * 新关注
     */
    public static final String NEW_SUBSCRIBE = "OVERVIEW:NEW_SUBSCRIBE";
    /**
     * 页面曝光
     */
    public static final String PAGE_EXPOSURE = "OVERVIEW:PAGE_EXPOSURE";
    /**
     * 商品曝光
     */
    public static final String GOODS_EXPOSURE = "OVERVIEW:GOODS_EXPOSURE";
    /**
     * 用户互动
     */
    public static final String INTERACTION = "INTERACTION:interaction_";

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MallBoomService mallBoomService;
    @Autowired
    private OverViewInfoService overViewInfoService;

    @Autowired
    private RequestFilter requestFilter;

    /**
     * 缓存用户互动
     * @param request
     */
    @GetMapping("/addinteraction")
    public void addInteraction(HttpServletRequest request){
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();

        String strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        redisTemplate.opsForSet().add(INTERACTION + strNow + "_" + orgId, customerId);
        redisTemplate.expire(INTERACTION + strNow + "_" + orgId,31,TimeUnit.DAYS);

        String key = INTERACTION + orgId + "_" + customerId;
        redisTemplate.opsForValue().set(key, "1", 30, TimeUnit.DAYS);
    }

    /**
     * 页面曝光
     */
    @GetMapping("/pageExposure")
    public void pageExposure(String name, Integer goodsId, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        if (wxUser == null) {
            return;
        }
        String orgId = wxUser.getOrgId();
        if (StringUtils.isEmpty(orgId)) {
            return;
        }
        if ("goods".equals(name)) {
            String belongId = mallBoomService.getGoodsBelongIdByGoodsId(goodsId);
            if (!StringUtils.isEmpty(belongId)) {
                orgId = belongId;
            }
            redisTemplate.opsForHash().increment(GOODS_EXPOSURE, orgId, 1);
        }
        redisTemplate.opsForHash().increment(PAGE_EXPOSURE, orgId, 1);
    }

    /**
     * 商品数据
     *
     * @param goodsId   商品id
     * @param enrollId  报名id
     * @param type      类型 1-点击\2-分享\3-购买(自己调用)\4-加入购物车\5-商品浏览时长\6-进入店铺
     * @param time      浏览时长(时分秒)
     * @param second    浏览时长(秒)
     * @param storeName 店铺名称
     * @param goodsNum  商品数量
     */
    @GetMapping("/goodsData")
    public void goodsData(String goodsId, String enrollId, Integer type, String time, Integer second,
                          String storeName, Integer goodsNum, String linkId,HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        if (wxUser == null) {
            return;
        }
        overViewInfoService.goodsData(goodsId, enrollId, type, time, second, storeName,
                goodsNum, null, wxUser.getCustomerId(), wxUser.getOrgId(),linkId);
    }

    @PostMapping("/goodsData2")
    public String goodsData2(
//            @RequestBody Map<String, Object> param,
            @RequestParam(defaultValue = "") String goodsId,
            @RequestParam(defaultValue = "") String enrollId,
            @RequestParam(defaultValue = "0") Integer type,
            @RequestParam(defaultValue = "") String time,
            @RequestParam(defaultValue = "0") Integer second,
            @RequestParam(defaultValue = "") String storeName,
            @RequestParam(defaultValue = "0") Integer goodsNum,
            @RequestParam(defaultValue = "") String customerId,
            @RequestParam(defaultValue = "") String linkId) {

//        String goodsId = (String) param.get("goodsId");
//        String enrollId = (String) param.get("enrollId");
//        Integer type = (Integer) param.get("type");
//        String time = (String) param.get("time");
//        Integer second = (Integer) param.get("second");
//        String storeName = (String) param.get("storeName");
//        Integer goodsNum = (Integer) param.get("goodsNum");
        String orgId = overViewInfoService.getOrgIdByCustomerId(customerId);
        overViewInfoService.goodsData(goodsId, enrollId, type, time, second, storeName,
                goodsNum, null, customerId, orgId,linkId);
        return "";
    }
}
